package com.jhws.business.advertisement.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jhws.business.advertisement.bo.AdvConstant;
import com.jhws.business.advertisement.bo.NoticeUpdateData;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.advertisement.bo.TblNoticeDimBo;
import com.jhws.common.base.bo.Global;
import com.jhws.common.Page;
import com.jhws.core.dataUpdate.service.DataUpdateService;
import com.jhws.core.db.BaseDao;
import com.jhws.business.advertisement.dao.ITblNoticeDimDAO;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.AccountsUtils;
import com.jhws.util.DateUtil;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;

import javax.annotation.Resource;

public class TblNoticeDimServiceSpringImpl implements ITblNoticeDimService {
    private ITblNoticeDimDAO tblNoticeDimDAO;

    @Resource
    private BaseDao BaseDao;
    @Resource
    protected ITblUserDimService IUserService;
    @Resource
    protected ITblCommDimService commDimService;
    @Resource
    private DataUpdateService dataUpdateService;

    public ITblNoticeDimDAO getTblNoticeDimDAO() {
        return tblNoticeDimDAO;
    }

    public void setTblNoticeDimDAO(ITblNoticeDimDAO tblNoticeDimDAO) {
        this.tblNoticeDimDAO = tblNoticeDimDAO;
    }


    public Page findTblNoticeDim(int start, int num, String whereClause)
            throws Exception {
        Page page = tblNoticeDimDAO.findTblNoticeDim(start, num, whereClause);
        return page;
    }


    public TblNoticeDimBo save(TblNoticeDimBo bo) throws Exception {
        return tblNoticeDimDAO.save(bo);
    }

    public List findTblNoticeDimByWhere(String whereClause) throws Exception {
        return tblNoticeDimDAO.findTblNoticeDimByWhere(whereClause);
    }

    public TblNoticeDimBo findTblNoticeDimById(String TblNoticeId) {
        return tblNoticeDimDAO.getById(TblNoticeId);
    }

    public void removeNoticeDim(String TblNoticeId) throws Exception {
        TblNoticeDimBo noticeDimBo = tblNoticeDimDAO.getById(TblNoticeId);
        Date date = new Date();
        long timeStamp = date.getTime();
        noticeDimBo.setTimestamp(timeStamp);
        noticeDimBo.setState(AdvConstant.STATE_CODE.delete.ordinal());
        BaseDao.h().update(noticeDimBo);
        //通知删除
        dataUpdateService.pushAndUpdate(timeStamp, NoticeUpdateData.NOTICE, noticeDimBo.getCommCode(), false, true, noticeDimBo.getTitile(), "");
    }

    /**
     * 查询资源列表
     *
     * @param adviceNum 设备编号
     * @param type      （type）
     * @return
     * @throws IOException
     */
    public List<Map<String, Object>> queryResourceList(String type, String adviceNum, String date) throws Exception {
        String sql = "SELECT\n" + "	 a.notice_code,\n" + "a.titile,\n" + "	a.context,\n" + "	a.create_time,\n" + "	a.begin_date,\n"
                + "	a.end_date" + " FROM\n" + "	tbl_notice_dim a\n" + "WHERE\n a.state = '0' ";

        if (AdvConstant.NOTICE_TYPE_COMM.equals(type)) {
            String currentDate = DateUtil.getTimeNowFmtDay();
            sql += " and a.notice_type = '" + AdvConstant.NOTICE_TYPE_COMM + "' " +
                    "and '" + currentDate + "' >= a.begin_date " +
                    "and '" + currentDate + "' <= a.end_date " +
                    "and a.comm_code = '" + adviceNum + "'";
        } else {
            String commCode = adviceNum.substring(0, 4);
            sql += " and ( " +
                    "(a.notice_type = '" + AdvConstant.NOTICE_TYPE_COMM + "' and a.comm_code = '" + commCode + "') " +
                    "or " +
                    "(a.notice_type = '" + AdvConstant.NOTICE_TYPE_ROOM + "' and a.equ_type = '" + adviceNum + "')" +
                    ") " +
                    "or " +
                    "(a.notice_type = '" + AdvConstant.NOTICE_TYPE_MOBILE + "')";
            //增量查询
            if (date != null) {
                sql += " and a.create_time > '" + date + "'";
            }
        }
        sql += " order by create_time asc";
        return BaseDao.j().queryForList(sql);
    }

    public Map<String, Object> findNoticeListPage(Map param) throws Exception {
        Map<String, Object> result = new HashMap<>();
        String whereClause = "state = '0' ";
        String commCode = MapUtils.getString(param, "commCode", "");
        if (commCode != null && StringUtils.isNotEmpty(commCode)) {
            whereClause += " and comm_code = '" + commCode + "'";
        } else {
            result.put("ret_code", -1);
            result.put("data", "没有选择小区！");
            return result;
        }

        String title = MapUtils.getString(param, "title", "");
        if (title != null && StringUtils.isNotEmpty(title)) {
            whereClause += " and titile like '%" + title + "%'";
        }

        String equType = MapUtils.getString(param, "equType", "");
        if (equType != null && StringUtils.isNotEmpty(equType)) {
            whereClause += " and equ_type like '%" + equType + "%'";
        }
        String sql = "select notice_code,titile,equ_type,create_time,notice_type,context," +
                "DATE_FORMAT(begin_date,'%Y-%m-%d') as begin_date,DATE_FORMAT(end_date,'%Y-%m-%d') as end_date from tbl_notice_dim where " + whereClause + " order by create_time desc";

        Page p = BaseDao.queryPage(sql, Map.class);
        List<Map<String, Object>> list = p.getList();
        for (Map<String, Object> map : list) {
            String noticeType = (String) map.get("notice_type");
            String putObject = "";
            if (AdvConstant.NOTICE_TYPE_COMM.equals(noticeType)) {
                TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(commCode);
                putObject += "【" + commDimBo.getCommName() + "】";
                long beginDate = DateUtil.convert2long(map.get("begin_date").toString(), DateUtil.DATE_FORMAT);
                long endDate = DateUtil.convert2long(map.get("end_date").toString(), DateUtil.DATE_FORMAT);
                long currentTime = DateUtil.convert2long(DateUtil.getTimeNowFmtDay(), DateUtil.DATE_FORMAT);
                if (beginDate <= currentTime && currentTime <= endDate) {
                    map.put("isShow", true);
                }
            } else if (AdvConstant.NOTICE_TYPE_ROOM.equals(noticeType)) {
                String commStrs = (String) map.get("equ_type");
                String comm = commStrs.substring(0, 4);
                TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(comm);
                putObject += "【" + commDimBo.getCommName() + "】" + AccountsUtils.getBuildingInfo(commStrs);
            }

            map.put("putObject", putObject);
        }

        result.put("list", list);
        result.put("size", p.getTotalNumber());
        result.put("ret_code", 1);
        return result;
    }

}
